package cn.zh.stan.adapter;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.ITem.CAItem;
import cn.zh.stan.MyApplication;
import cn.zh.stan.R;
import cn.zh.stan.activity.DtailGoods;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

import static cn.zh.stan.util.PullToRefreshLayout.TAG;

/**
 * Created by WYN on 2018/1/29 0029.
 */

public class CarAdapter extends BaseAdapter{
     Context context;
     ArrayList<CAItem> arrayList;
     LayoutInflater inflater;
    CheckBox allchose;
    Dialog dialog;
    MyApplication mapp;

    int index = -1;

     public CarAdapter(Context context,ArrayList<CAItem> arrayList,CheckBox allchose,MyApplication mapp){
        this.context=context;
        this.arrayList=arrayList;
        inflater=LayoutInflater.from(context);
        this.allchose=allchose;
        this.mapp=mapp;
        }


    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return arrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
         Holder holder=null;
         if (null==view){
          holder=new Holder();
          view=inflater.inflate(R.layout.calayout,null,false);
          holder.cbox=view.findViewById(R.id.cbox);
          holder.caimg=view.findViewById(R.id.caimg);
          holder.reduce=view.findViewById(R.id.reduce);
          holder.cadd=view.findViewById(R.id.cadd);
          holder.cttxt=view.findViewById(R.id.cttxt);
          holder.cuint=view.findViewById(R.id.cuint);
          holder.caprice=view.findViewById(R.id.caprice);
          holder.cetxt=view.findViewById(R.id.cetxt);
          view.setTag(holder);
          }else {
             holder= (Holder) view.getTag();
           }
          holder.cttxt.setText(arrayList.get(i).getGoodsName());
          holder.cuint.setText("单位:"+arrayList.get(i).getUnit());
          holder.caprice.setText("¥"+arrayList.get(i).getPrice_Detail());
          holder.cetxt.setText(arrayList.get(i).getNum()+"");

          if (arrayList.get(i).isType()){
              holder.cbox.setChecked(true);
          }else {
              holder.cbox.setChecked(false);
            }

        final Holder finalHolder = holder;
        holder.reduce.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if (arrayList.get(i).getNum()>1) {
                      int a = arrayList.get(i).getNum() - 1;
                      arrayList.get(i).setNum(a);
                      finalHolder.cetxt.setText(a+"");
                  }else {
                      getDialog(i);
                  }
                  }
              });
          holder.cadd.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {

                  int b= arrayList.get(i).getNum()+1;
                  arrayList.get(i).setNum(b);
                  finalHolder.cetxt.setText(b+"");
                  }
          });


          holder.cbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                  if (b){
                      arrayList.get(i).setType(true);
                    }else {
                      arrayList.get(i).setType(false);
                     }
                  int a=0;
                  for (int i=0;i<arrayList.size();i++){
                     if (arrayList.get(i).isType()){
                         a=a+1;
                      }
                   }
                 if (a==arrayList.size()){
                     mapp.setIschose(false);
                     allchose.setChecked(true);
                     mapp.setIschose(true);
                    }else {
                     if (allchose.isChecked()){
                         mapp.setIschose(false);
                         allchose.setChecked(false);
                         mapp.setIschose(true);
                       }
                    }
                }
             });
        //Editext设置


        return view;
        }



    class  Holder {
      CheckBox cbox;
      ImageView caimg,reduce,cadd;
      TextView cttxt,cuint,caprice;
      TextView  cetxt;
       }


    public void getDialog(final int position){
        View v = inflater.inflate(R.layout.delete, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v
                .findViewById(R.id.delete);// 加载布局

        dialog = new Dialog(context, R.style.MyDialogStyle);
        dialog.show();
        dialog.setContentView(layout,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        v.findViewById(R.id.ocancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        v.findViewById(R.id.osure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //s删除地址网络请求
                String id=arrayList.get(position).getId();
                dcdress(id,position);
            }
        });
      }





    public void dcdress(String id, final int position){
        OkHttpUtils.post(BaseUrl.goods_delecar)
                .params("id",id)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                                arrayList.remove(position);
                                ToastUtil.showLong(context,"删除成功");
                                notifyDataSetChanged();
                                }
                            dialog.dismiss();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
              }
           }
